package team.dmh.wx.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import team.dmh.wx.entity.Friend;
import team.dmh.wx.entity.FriendRequest;
import team.dmh.wx.entity.User;
import team.dmh.wx.exception.ExceptionEnum;
import team.dmh.wx.exception.MyException;
import team.dmh.wx.service.IFriendRequestService;
import team.dmh.wx.service.IFriendService;
import team.dmh.wx.service.IUserService;
import team.dmh.wx.util.Result;
import team.dmh.wx.util.ResultUtils;
import team.dmh.wx.vo.UserVO;

import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hzk
 * @since 2018-10-15
 */
@Api(tags = "好友搜索、列表、备注")
@RestController
@RequestMapping("/user")
@Slf4j
public class FriendController {

    @Autowired
    private IFriendService friendService;

    @Autowired
    private IFriendRequestService friendRequestService;

    @Autowired
    private IUserService userService;

    @ApiOperation("查询好友列表")
    @GetMapping("/myFriendsWithSorted")
    public Result myFriendsWithSorted(String userId) {
        return ResultUtils.success(friendService.getFriendVOMap(userId));
    }

    @ApiOperation("查询好友列表不排序")
    @GetMapping("/myFriends")
    public Result myFriends(String userId){
        return ResultUtils.success(friendService.getFriendVOList(userId));
    }

    @ApiOperation("用于APP搜索用户信息(进行好友检查)")
    @PostMapping("/search")
    public Result searchUser(@RequestBody @ApiParam(value = "userId(用户个人id)、friendUsername(需要查询的用户的用户名)") Map<String, String> params) {
        String userId = params.get("userId");
        String friendUsername = params.get("friendUsername");
        friendService.check(userId, friendUsername);
        UserVO friendVO = new UserVO();
        User friendInfo = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, friendUsername));
        BeanUtils.copyProperties(friendInfo, friendVO);
        return ResultUtils.success(friendVO);
    }

    @ApiOperation("用于网页搜索用户信息(不进行好友检查)")
    @GetMapping("/searchWithoutCheck")
    public Result searchUserWithoutCheck(String friendUsername) {
        User friendInfo = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, friendUsername));
        if (friendInfo == null) {
            throw new MyException(ExceptionEnum.USER_NOT_EXIST);
        }else {
            UserVO friendVO = new UserVO();
            BeanUtils.copyProperties(friendInfo, friendVO);
            return ResultUtils.success(friendVO);
        }
    }

    @ApiOperation("设置好友备注")
    @PostMapping("/setAlias")
    public Result setAlias(@RequestBody Map<String,String> params){
        String userId=params.get("userId");
        String friendId=params.get("friendId");
        String alias=params.get("alias");
        friendService.update(new Friend().setFriendAlias(alias),
                new LambdaQueryWrapper<Friend>()
                        .eq(Friend::getUserId,userId)
                        .eq(Friend::getFriendId,friendId));
        return ResultUtils.success().data(friendService.getFriendVOList(userId));
    }

    @ApiOperation("删除好友")
    @GetMapping("/deleteFriend")
    public Result deleteFriend(String userId,String friendId){
        friendService.remove(new LambdaQueryWrapper<Friend>().eq(Friend::getUserId,userId).eq(Friend::getFriendId,friendId));
        friendRequestService.remove(new LambdaQueryWrapper<FriendRequest>().eq(FriendRequest::getSendUserId,friendId).eq(FriendRequest::getAcceptUserId,userId));
        return ResultUtils.success("删除成功！").data(friendService.getFriendVOList(userId));
    }

}
